
# Setup -------------------------------------------------------------------

options("max.print" = 1000, digits=4, scipen = 999)

# load data ---------------------------------------------------------------

btw_main <- read_csv("data/cleaned/btw_main_fin.csv") %>% mutate(across(c("time_id", "ags_num"), as.integer))
btw_west <- btw_main %>% filter(state_id < 11) %>% as.data.frame
btw_east <- btw_main %>% filter(state_id >= 11) %>% as.data.frame


# Defining universal params -----------------------------------------------

# lags
lags <- 
    c(rep(rep(1, 7), 4),
      rep(rep(3, 7), 4))

# this is to indicate whether for 
# a specific matched set we use refinement.
# this is not required by the function, but 
# will be used to name the matched sets object 
# this way, bookkeeping is made easier

whether_refinements <- rep(c("no", "no", "yes", "yes", "yes", "yes",
                                     "yes"), 8)


# the refinment methods
# note that later we will substitute "CBPS.msm.weight" for "CBPS.weight"
refinement.methods <- rep(c("none", "mahalanobis", "mahalanobis", "mahalanobis", "CBPS.match", "CBPS.match",
                                    "CBPS.weight"), 8)
match.missings <- rep(TRUE, 56) # whether we match on missing data, currently we don't
listwise.deletes <- rep(FALSE, 56) # otherwise we can't match on missingness


# qois
atts <- rep("att", 56)
atcs <- rep("atc", 56)

# leads
leads <- rep(c(rep(0,7), rep(1,7), rep(2,7), 
                       rep(3,7)), 2) # the choice of leads


forbid.treatment.reversals <- rep(F, 56) # currently we allow treatment reversal, later will forbid
verboses <- rep(T, 56)

use.diagonal.variance.matrixs <- rep(T, 56) # we use diagonal variance matrices for Maha
exact.match.variables <- lapply(as.list(rep(0, 56)), 
                                function(x) x <- NULL)

# Whether we match on treatment history - we will present balances without matching on history to demonstrate
# the utility of matching. So there is an "F" in addition to the 6 "T"s. 
matchings <- rep(c(F,T,T,T,T,T,T),8)

time.ids <- rep("time_id", 56) # time ids
unit.ids <- rep("ags_num", 56) # unit ids
treatments_entry <- rep("greens_any_entry_st_parl", 56) # treatment
treatments_exit <- rep("greens_exit_st_parl", 56) # treatment alt


# match sizes
outcome.vars <- rep("delta_cdu_csu", 56) # outcome variables


# Getting matched sets ----------------------------------------------------


# West --------------------------------------------------------------------

size.matches_w <- rep(c(10, 5, 5, 10, 5, 10, 10), 8)


# formulae for both matching on 1970s outcomes and all censuses
covs.formulae <- lapply(c(
    rep("~ I(lag(cdu_csu_zweit_sh, 1)) + I(lag(turnout, 1)) + I(lag(pop_dens, 1)) + I(lag(emp_sh, 1)) + I(lag(log_gdp_capita_pps, 1)) + 
    I(lag(log_gva, 1)) + I(lag(agri_emp_sh, 1)) + I(lag(manu_gva_sh, 1)) ", 28), 
    rep("~ I(lag(cdu_csu_zweit_sh, 1:3)) + I(lag(turnout, 1:3)) + I(lag(pop_dens, 1:3)) + I(lag(emp_sh, 1:3)) + I(lag(log_gdp_capita_pps, 1:3)) + 
    I(lag(log_gva, 1:3)) + I(lag(agri_emp_sh,1:3)) + I(lag(manu_gva_sh, 1:3)) ", 28)),
    as.formula)


#+ pop_log_70 + pop_sh_male_70 + pop_sh_catho_70 + pop_sh_students_70 + erwerb_sh_agri_70 + pop_sh_over65_70 + taxes_per_cap_70


params_west_entry <-
    list(lag = lags, 
         time.id = time.ids,
         unit.id = unit.ids,
         treatment = treatments_entry,
         refinement.method = refinement.methods,
         match.missing = match.missings,
         listwise.delete = listwise.deletes,
         covs.formula = covs.formulae,
         size.match = size.matches_w,
         qoi = atts,
         outcome.var = outcome.vars,
         lead = leads,
         forbid.treatment.reversal = forbid.treatment.reversals,
         verbose = verboses,
         # listwise.delete = list(F,F, F, F, F),
         use.diagonal.variance.matrix = use.diagonal.variance.matrixs,
         exact.match.variables = exact.match.variables,
         matching = matchings
    )


# covariates
covariates_greens_west = c("cdu_csu_zweit_sh", "turnout", "pop_dens", 
                           "emp_sh", "log_gdp_capita_pps", "log_gva",
                           "agri_emp_sh", "manu_gva_sh")



# Getting matched sets for ATT, allowing for treatment reversal


greens_west_entry <- pmap(params_west_entry, PanelMatch, data = btw_west)
names(greens_west_entry) <- paste(unlist(lags), 
                        unlist(leads), 
                        unlist(whether_refinements),
                        unlist(refinement.methods),
                        unlist(size.matches_w), matchings, atts) # naming the list


# East --------------------------------------------------------------------

size.matches_e <- rep(c(10, 5, 5, 10, 5, 10, 10), 8)

# parameters specific to East 

covs.formulae_entry <- as.list(lapply(c(
    rep("~ I(lag(cdu_csu_zweit_sh, 1)) + I(lag(turnout, 1)) + I(lag(pop_dens, 1)) + 
    I(lag(emp_sh, 1)) + I(lag(log_gdp_capita_pps, 1)) + 
    I(lag(log_gva, 1)) + I(lag(agri_emp_sh,1)) + 
    I(lag(manu_gva_sh, 1))", 28), 
    rep("~ I(lag(cdu_csu_zweit_sh, 1:2)) + I(lag(turnout, 1:3)) + I(lag(pop_dens, 1:2)) + 
    I(lag(emp_sh, 1:3)) + I(lag(log_gdp_capita_pps, 1:3)) + 
    I(lag(log_gva, 1:3)) + I(lag(agri_emp_sh,1:3)) + 
    I(lag(manu_gva_sh, 1:3))", 28)),
    as.formula))



covariates_east <- c("cdu_csu_zweit_sh", "turnout", "pop_dens", "log_gva",
                   "emp_sh", "log_gdp_capita_pps", "agri_emp_sh", "manu_gva_sh"
)


params_east_entry <-
    list(lag = lags, 
         time.id = time.ids,
         unit.id = unit.ids,
         treatment = treatments_entry,
         refinement.method = refinement.methods,
         match.missing = match.missings,
         listwise.delete = listwise.deletes,
         covs.formula = covs.formulae_entry,
         size.match = size.matches_e,
         qoi = atts,
         outcome.var = outcome.vars,
         lead = leads,
         forbid.treatment.reversal = forbid.treatment.reversals,
         verbose = verboses,
         # listwise.delete = list(F,F, F, F, F),
         use.diagonal.variance.matrix = use.diagonal.variance.matrixs,
         exact.match.variables = exact.match.variables,
         matching = matchings
    )


## Getting matched sets for ATT in the east, allowing for treatment reversal
greens_east_entry <- pmap(params_east_entry, PanelMatch, data = btw_east)

names(greens_east_entry) <- paste(unlist(lags), 
                                  unlist(leads), 
                                  unlist(whether_refinements),
                                  unlist(refinement.methods),
                                  unlist(size.matches_e), matchings, atts) # naming the lists


